package com.findroader.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.findroader.blog.dao.ArticleCommentsDao;
import com.findroader.blog.dao.ArticleDao;
import com.findroader.blog.dao.ArticleTypeDao;
import com.findroader.blog.dto.HomeArticleDTO;
import com.findroader.blog.dto.QueryParamDTO;
import com.findroader.blog.dto.ReplyContent;
import com.findroader.blog.model.ArticleBean;
import com.findroader.blog.model.ArticleCommentsBean;
import com.findroader.blog.model.ArticleTypeBean;
import com.findroader.blog.page.Page;
import com.findroader.blog.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Resource
	private ArticleDao articleDao;

	@Resource
	private ArticleCommentsDao commentDao;

	@Resource
	private ArticleTypeDao typeDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.findroader.blog.service.impl.ArticleService#save(com.findroader.blog
	 * .model.ArticleBean)
	 */
	public int save(ArticleBean article) {
		return articleDao.save(article);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.findroader.blog.service.impl.ArticleService#update(com.findroader
	 * .blog.model.ArticleBean)
	 */
	public int update(ArticleBean article) {
		return articleDao.update(article);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.findroader.blog.service.impl.ArticleService#delete(int)
	 */
	public int delete(int id) {
		return articleDao.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.findroader.blog.service.impl.ArticleService#get(int)
	 */
	public ArticleBean get(int id) {
		return articleDao.getArticle(id);
	}

	public Page<ArticleBean> queryArticleList(Page<ArticleBean> page,
			Map<String, Object> param) {
		page.setTotalRecords(articleDao.queryArticleCount());
        
		QueryParamDTO queryParam = new QueryParamDTO();
        queryParam.setStart(page.getFirstResult()-1);
        queryParam.setEnd(page.getPageSize());
		page.setResult(articleDao.queryArticleList(queryParam));
		
		return page;
	}

	public Page<HomeArticleDTO> queryArticleList(Page<HomeArticleDTO> page,
			QueryParamDTO queryParam) {
		page.setTotalRecords(articleDao.queryArticleCount());
		queryParam.setStart(page.getFirstResult());
		queryParam.setEnd(page.getPageSize());
		List<ArticleBean> list = articleDao.queryArticleList(queryParam);
		Map<Integer, ArticleBean> articles = new HashMap<Integer, ArticleBean>();
		for (ArticleBean a : list) {
			articles.put(a.getArticleId(), a);
		}
		List<HomeArticleDTO> commentCount = articleDao
				.queryArticleCommentsCount();
		for (HomeArticleDTO h : commentCount) {
			if (articles.containsKey(h.getArticleId())) {
				h.setArticle(articles.get(h.getArticleId()));
			}
		}

		page.setResult(commentCount);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.findroader.blog.service.impl.ArticleService#comment(com.findroader
	 * .blog.model.ArticleCommentsBean)
	 */
	public int comment(ArticleCommentsBean comments) {
		return commentDao.saveComments(comments);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.findroader.blog.service.impl.ArticleService#reply(java.lang.String,
	 * int)
	 */
	public int reply(String reply, int commentId) {
		ReplyContent content = new ReplyContent();
		content.setCommentId(commentId);
		content.setReply(reply);
		
		return commentDao.replyComments(content);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.findroader.blog.service.impl.ArticleService#saveArticleType(com.
	 * findroader.blog.model.ArticleTypeBean)
	 */
	public int saveArticleType(ArticleTypeBean type) {
		return typeDao.save(type);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.findroader.blog.service.impl.ArticleService#updateType(com.findroader
	 * .blog.model.ArticleTypeBean)
	 */
	public int updateType(ArticleTypeBean type) {
		return typeDao.update(type);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.findroader.blog.service.impl.ArticleService#deleteType(int)
	 */
	public int deleteType(int key) {
		return typeDao.delete(key);
	}

	public ArticleTypeBean getType(String typeKey) {
		return typeDao.get(typeKey);
	}

	public List<ArticleTypeBean> queryTypeList(){
		return typeDao.queryList();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.findroader.blog.service.impl.ArticleService#deleteComment(int)
	 */
	public int deleteComment(int commentId) {
		return commentDao.delete(commentId);
	}

	@Override
	public Page<ArticleCommentsBean> queryAllComments(
			Page<ArticleCommentsBean> page, QueryParamDTO param) {
		
		param.setStart(page.getFirstResult());
		param.setEnd(page.getPageSize());
		page.setTotalRecords(commentDao.queryAllCommentsCount());
		List<ArticleCommentsBean> result =commentDao.queryAllComments(param);
	    page.setResult(result); 
		return page;
	}
}
